package com.china08.yxyapi.api.manage.sch.platform.website.section;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SectionDelJudgeController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @ApiOperation("删除栏目")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/section/del/judge", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String sectionId = reqModel.sectionId;
        String schoolId = getProfile(request, response).getSchoolId();

        RespModel respModel = new RespModel();

        SwsSection swsSection = swsSectionRepository.findById(sectionId);
        Assert.isTrue(null != swsSection, "不存在要删除的栏目");

        String pid = swsSection.getPid();

        // 删除的是一级栏目
        if (pid.equals("0")) {

            // 获取当前栏目下面的二级栏目集合
            List<SwsSection> section2List = swsSectionRepository.findBySchoolIdAndPidOrderByOrderNumAsc(schoolId, sectionId);
            if (section2List.size() > 0) {
                respModel.msg = "有子栏目，请先删除子栏目";
                respModel.status = "0";
                respModel.success = false;
            }

            // 没有二级栏目，判断是否有文章
            else {
                List<SwsArticle> list = swsArticleRepository.findBySection1Id(sectionId);

                // 二级栏目下面有文章
                if (list.size() > 0) {
                    respModel.msg = "您确定删除栏目吗？删除栏目后栏目下的资讯将没有所属栏目，请将栏目下内容移至其他栏目";
                    respModel.status = "1";
                    respModel.success = false;
                }
                // 没有文章
                else {
                    respModel.msg = "您确定删除栏目吗？";
                    respModel.status = "2";
                    respModel.success = true;
                }
            }
        }
        // 删除二级栏目，判断是否有文章
        else {
            List<SwsArticle> list = swsArticleRepository.findBySection2Id(sectionId);

            // 没有文章，可以删除
            if (list.size() == 0) {
                respModel.msg = "您确定删除栏目吗？";
                respModel.status = "2";
                respModel.success = true;
            }
            // 有文章
            else {
                respModel.msg = "您确定删除栏目吗？删除栏目后栏目下的资讯将移至该栏目的一级栏目下面";
                respModel.status = "3";
                respModel.success = false;
            }
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String sectionId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(sectionId), "未获取到要删除的栏目");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        // 是否可以删除
        public boolean success = true;
        // 提示信息
        public String msg;

        public String status;
    }
}
